package day03

import scala.util.Random

/**
  * 模式匹配
  */
class CaseDeamo {

}

/**
  * 样例类用于做模式匹配，封装数据（多例），不用new即可创建实例
  */

case class SubmitTask(id:String,name:String)
case class HeartBeat(time:Long)

//样例对象，模式匹配（单例）
case object CheckTimeOutTask

object CaseDeamo{

  def main(args: Array[String]): Unit = {
    case6
  }

  /**
    * 匹配 option 进行模式匹配 匹配只能是 some 或者是 none
    */
  def case8: Unit ={

  }

  /**
    * 匹配样例类
    */
  def case7: Unit ={
    val arr = Array(CheckTimeOutTask,HeartBeat(1000),SubmitTask("id1","name1"),SubmitTask("id2","name2"))
    val i: Int = Random.nextInt(arr.length)
    val c: Product with Serializable = arr(i)
    c match {
      case CheckTimeOutTask => println("匹配到了CheckTimeOutTask")
    }

  }

  /**
    * 匹配元组
    */
  def case6: Unit ={
    val t1 = ("hello",1,1.0)
    t1 match {
      case (a,b,c) => println(s"$a,$b$c")
      case (_,_,_) => println("case2")
      case _ => println("_")
    }
  }

  /**
    * 匹配list集合
    * s可以在里面使用变量
    */
  def case5: Unit ={
    val arr = List(1,2,3,4)
    arr match {
      case 1 :: Nil => println("case1")
      case 1::a => println(s"hello $a")
    }

  }

  /**
    * 匹配数组数据（相当于加上约束）
    */
  def case4: Unit ={
    val arr = Array(1,"hello")
    val Array(age,name) = arr

    println(age)
    println(name)
  }

  /**
    * 匹配数组
    */
  def case3: Unit ={
    val arr = Array(1,1,3,1)

    arr match {
      case Array(1,1,x,y) =>{
        println("case1")
      }
      case Array(1,1,x,y) =>{
          println(x+y)
      }
      case Array(1,1,x,y) =>{
        println(x+y)
      }
      case _ =>{
        println("hello")
      }

    }
  }

  /**
    * 匹配类型的
    */
  def case2: Unit ={
    val arr = Array("hello",1,"word",1.2)
    val rand = Random.nextInt(4)
    val i = arr(rand)
    println(i)
    i match {
      case z:Int if(z>1) => {
        println("int类型的："+z)
      }
      case y:String =>{
        println("String:"+y)
      }
      case z:Any =>{
        println(z)
      }
    }
  }

  /**
    * 匹配内容的
    */
  def case1: Unit ={
    val arr = Array("hello","hi","word")
    val rand = Random.nextInt(3)
    val i = arr(rand)
    println(i)
    i match {
      case "hello" => println("hello")
      case "hi" => println("hi")
      case _ => println("other")
    }
  }
}
